<?php

namespace application\admin\controller;
use application\common\controller\Base;
use think\Db;
use application\common\controller\Tools;
use application\common\model\ExamineeInfo;
use application\common\model\Notify;

class Exam extends Base
{
    public function examInfo() {
        $sql = 'SELECT cfe_exam.exam_id, cfe_exam.exam_name, cfe_exam.status, cfe_exam.isZiped, cfe_user.user_name FROM cfe_exam, cfe_user WHERE cfe_exam.exam_creator_id = cfe_user.user_id';
        $info = Db::query($sql);
        $this->assign([
            'active' => [3,31],
            'title' => '考试信息',
            'messageCount' => 0,
            'info' => $info,
        ]);
        return $this->fetch();
    }
    public function cleanExam($key) {
        $trueId = $key - 12854;
        $exam = new \application\common\model\Exam();
        $examInfo = $exam->where('exam_id', $trueId)->select()[0];
        if ($examInfo['status'] == '1') {
            $this->error("考试正在进行中不能删除！", $_SERVER['HTTP_REFERER']);
        }elseif ($examInfo['isZiped'] != '1') {
            $this->error("打包下载学生提交信息后才能清理考试！", $_SERVER['HTTP_REFERER']);
        } else {
            ExamineeInfo::destroy(['exam_id' => $examInfo['exam_id']]);
            Notify::destroy(['exam_id' => $examInfo['exam_id']]);
            $paperPath = ROOT_PATH.'uploads'.DS.'paper'.DS.$examInfo['exam_name'];
            $answerPath = ROOT_PATH.'uploads'.DS.'answer'.DS.$examInfo['exam_name'];
            if (is_dir($paperPath)) Tools::removeDir($paperPath);
            if (is_dir($answerPath)) Tools::removeDir($answerPath);
            $exam->save(['status' => '3'], ['exam_id' => $examInfo['exam_id']]);
            $this->redirect($_SERVER['HTTP_REFERER']);
        }
    }
    public function deleteExam($key) {
        $trueId = $key - 34542;
        \application\common\model\Exam::destroy(['exam_id' => $trueId]);
        $this->redirect($_SERVER['HTTP_REFERER']);
    }
}


?>